package cn.kooki.app.duobao.ui.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.share.NotShareGood;
import cn.kooki.app.duobao.data.Bean.share.ShareListItem;
import cn.kooki.app.duobao.util.ListUtils;
import cn.kooki.app.duobao.util.StringUtils;
import cn.kooki.app.duobao.util.TimeUtils;

/**
 * Created by xiong on 2016/1/1.
 */
public class MyShareAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int GOOD_TYPE = 4;
    public static final int SHARE_TYPE = 5;
    private ArrayList<ShareListItem> shares;
    private ArrayList<NotShareGood> goods;
    private Context context;

    public interface OnTypeItemClickListener {
        void onItemClick(int type, int position);
    }

    private OnTypeItemClickListener itemClickListener;

    public void setItemClickListener(OnTypeItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public MyShareAdapter(Context context, ArrayList<ShareListItem> shares, ArrayList<NotShareGood> goods) {
        this.context = context;
        this.shares = shares;
        this.goods = goods;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == GOOD_TYPE) {
            View view = LayoutInflater.from(context).inflate(R.layout.layout_myshare, parent, false);
            GoodViewHolder goodViewHolder = new GoodViewHolder(view);
            return goodViewHolder;
        } else if (viewType == SHARE_TYPE) {
            View itemview = LayoutInflater.from(context).inflate(R.layout.layout_share_item, parent, false);
            ShareViewHolder shareViewHolder = new ShareViewHolder(itemview);
            return shareViewHolder;
        } else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        if (position < goods.size()) {
            GoodViewHolder viewHolder = (GoodViewHolder) holder;
            NotShareGood notShareGood = goods.get(getTypePosition(position));
            Glide.with(context).load(notShareGood.thumb).fitCenter().into(viewHolder.goodImage);
            viewHolder.goodName.setText(notShareGood.title);
            viewHolder.share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onItemClick(GOOD_TYPE, getTypePosition(position));
                    }
                }
            });
        } else {

            ShareViewHolder viewHolder = (ShareViewHolder) holder;
            ShareListItem item = shares.get(getTypePosition(position));
            viewHolder.shareListTitle.setText(item.title);
            viewHolder.shareListGoodName.setText(item.shopname1);
            viewHolder.shareListContent.setText(item.content);
            int status = StringUtils.nullSafeParseInt(item.status);
            if (status == 1) {
                viewHolder.shareListStatus.setText("已审核");
                viewHolder.shareListStatus.setBackgroundResource(R.color.green);
            } else if (status == 0) {
                viewHolder.shareListStatus.setText("未审核");
                viewHolder.shareListStatus.setBackgroundResource(R.color.wait);
            } else {
                viewHolder.shareListStatus.setText("审核未通过");
                viewHolder.shareListStatus.setBackgroundResource(R.color.gray);
            }
            viewHolder.shareListTime.setText(TimeUtils.getMouthTime(item.time));
            if (!ListUtils.isEmpty(item.photolist)) {
                Glide.with(context).load(item.photolist.get(0)).centerCrop().into(viewHolder.shareListPreview);
            }
            viewHolder.shareListWrapper.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.onItemClick(SHARE_TYPE, getTypePosition(position));
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < goods.size()) {
            return GOOD_TYPE;
        } else if (position < shares.size() + goods.size()) {
            return SHARE_TYPE;
        }
        return super.getItemViewType(position);
    }

    public int getTypePosition(int position) {
        if (position < goods.size()) {
            return position;
        } else {
            return position - goods.size();
        }
    }


    @Override
    public int getItemCount() {
        return goods.size() + shares.size();
    }


    class ShareViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.share_list_time)
        TextView shareListTime;
        @Bind(R.id.share_list_title)
        TextView shareListTitle;
        @Bind(R.id.share_list_status)
        TextView shareListStatus;
        @Bind(R.id.share_list_preview)
        ImageView shareListPreview;
        @Bind(R.id.share_list_goodName)
        TextView shareListGoodName;
        @Bind(R.id.share_list_content)
        TextView shareListContent;
        @Bind(R.id.share_list_wrapper)
        LinearLayout shareListWrapper;

        public ShareViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    class GoodViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.good_image)
        ImageView goodImage;
        @Bind(R.id.good_name)
        TextView goodName;
        @Bind(R.id.share)
        Button share;

        public GoodViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
